<?php
require_once("lib/api.php");
require_once("res/const.php");
require_once("lib/auth/session.php");
$api = new api();

if (!loggedIn()) {
	$api->setYouFail("Not logged in.");
} elseif (isset($_POST['postid']) && isset($_POST['content'])) {
	require_once("lib/post/view.php");
	$postid = $_POST['postid'];
	if (!is_numeric($postid)) {
		$api->setYouFail("Postid needs to be a number.");
	} elseif (!($postid > 0)) {
		$api->setYouFail("Postid needs to be a number larger than 0.");
	} elseif (!getPost($postid)) {
		$api->setYouFail("Post does not exist.");
	} else {
		$content = filter_var($_POST['content'], FILTER_SANITIZE_STRING);
		if (!(strlen($content) < $MAX_LENGTH_CONTENT)) {
			$api->setYouFail("Content needs to be shorter than $MAX_LENGTH_CONTENT characters.");
		} elseif (!(strlen($content) > $MIN_LENGTH_CONTENT)) {
			$api->setYouFail("Content needs to be longer than $MIN_LENGTH_CONTENT characters.");
		} else {
			require_once("lib/comment/create.php");
			createComment($postid, $content);
			$api->setOk();
		}
	}
} else {
	$api->setYouFail("Both content and postid need to be set.");
}
$api->returnResponse();

?>